﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
 
using System.Text;
using System.Windows.Forms;
using FarPoint.Win.Spread.CellType;

namespace Demo
{
    public partial class previewRow : DemoBaseUserControl
    {
        public previewRow()
        {
            InitializeComponent();
            this.Title = "行预览";
            this.Description = "Spread .NET  提供行预览功能，以便更轻松的获得行信息。预览行显示在它所提示的行的下面。同时可以指定预览行的颜色和其他格式。";

        }

        private void previewRow_Load(object sender, EventArgs e)
        {
            CreateData();
            FpSpread1.ActiveSheet.Rows.Count = 20;
            FpSpread1.ActiveSheet.Columns.Count = 10;
            FpSpread1.ActiveSheet.PreviewRowInfo.Visible = true;
            FpSpread1.ActiveSheet.PreviewRowInfo.ColumnIndex = 1;
            FpSpread1.ActiveSheet.PreviewRowInfo.BackColor = Color.LightGreen;
            for (int i = 0; i < this.FpSpread1.Sheets[0].Columns.Count; i++)
            {
                this.FpSpread1.Sheets[0].Columns[i].Width = 100;
            }

            numericUpDownColumnIndex.Minimum = 0;
            numericUpDownColumnIndex.Maximum = FpSpread1.ActiveSheet.Columns.Count - 1;
            ckVisible.Checked = FpSpread1.ActiveSheet.PreviewRowInfo.Visible;
            this.cboAlignment.SelectedIndex = 0;
        }
        public void CreateData()
        {

            string defaultCommonPath = System.IO.Path.Combine(Application.StartupPath, "..\\..\\Resources\\");// default XML path
            string commonPath1 = defaultCommonPath;

            Microsoft.Win32.RegistryKey commonKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("FarPoint.Win.Spread\\Resources");
            if (commonKey != null)
            {
                object commonPathObj = commonKey.GetValue("v52005", defaultCommonPath);
                commonPath1 = commonPathObj is string ? (string)commonPathObj : defaultCommonPath;
            }

            string imag1 = System.IO.Path.Combine(System.IO.Path.Combine(commonPath1, "CellTypes"), "flag_blue.jpg");
            string imag2 = System.IO.Path.Combine(System.IO.Path.Combine(commonPath1, "CellTypes"), "flag_green.jpg");
            string imag3 = System.IO.Path.Combine(System.IO.Path.Combine(commonPath1, "CellTypes"), "flag_red.jpg");

            Bitmap img1 = global::Demo.Properties.Resources.flag_blue;
            Bitmap img2 = global::Demo.Properties.Resources.flag_green;
            Bitmap img3 = global::Demo.Properties.Resources.flag_red;

            //Create the cell types 
            FarPoint.Win.Spread.CellType.GeneralCellType imgType = new FarPoint.Win.Spread.CellType.GeneralCellType();
            imgType.BackgroundImage = new FarPoint.Win.Picture(img1, FarPoint.Win.RenderStyle.Normal, Color.White, FarPoint.Win.HorizontalAlignment.Center, FarPoint.Win.VerticalAlignment.Center);

            FarPoint.Win.Spread.CellType.GeneralCellType imgType2 = new FarPoint.Win.Spread.CellType.GeneralCellType();
            imgType2.BackgroundImage = new FarPoint.Win.Picture(img2, FarPoint.Win.RenderStyle.Normal, Color.White, FarPoint.Win.HorizontalAlignment.Center, FarPoint.Win.VerticalAlignment.Center);

            FarPoint.Win.Spread.CellType.GeneralCellType imgType3 = new FarPoint.Win.Spread.CellType.GeneralCellType();
            imgType3.BackgroundImage = new FarPoint.Win.Picture(img3, FarPoint.Win.RenderStyle.Normal, Color.White, FarPoint.Win.HorizontalAlignment.Center, FarPoint.Win.VerticalAlignment.Center);

            int i = 0;
            //Apply the cell type images 
            {
                for (i = 0; i <= FpSpread1.ActiveSheet.RowCount - 1; i += 3)
                {
                    FpSpread1.ActiveSheet.Cells[i, 0].CellType = imgType;
                }

                for (i = 1; i <= FpSpread1.ActiveSheet.RowCount - 1; i += 3)
                {
                    FpSpread1.ActiveSheet.Cells[i, 0].CellType = imgType2;
                }

                for (i = 2; i <= FpSpread1.ActiveSheet.RowCount - 1; i += 3)
                {
                    FpSpread1.ActiveSheet.Cells[i, 0].CellType = imgType3;
                }
            }


            {
                //Column 0 
                //Image 
                //Set Column Widths 
                FpSpread1.ActiveSheet.Columns[0].Width = 30;

                //Column 1 - Product 
                //Set as check box 
                FpSpread1.ActiveSheet.Columns[1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Left;
                FpSpread1.ActiveSheet.Columns[1].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                FpSpread1.ActiveSheet.Cells[0, 1, FpSpread1.ActiveSheet.RowCount - 1, 1].Font = new Font("微软雅黑", 10, FontStyle.Bold);
                FpSpread1.ActiveSheet.Columns[1].Width = 220;

                //Column 2 - In Stock 
                //Set as check box 
                FpSpread1.ActiveSheet.Columns[2].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                FpSpread1.ActiveSheet.Columns[2].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                FpSpread1.ActiveSheet.Cells[0, 2, FpSpread1.ActiveSheet.RowCount - 1, 2].CellType = new CheckBoxCellType();
                FpSpread1.ActiveSheet.Columns[2].Width = 42;

                //Column 3 - Qty 
                //Set as integer 
                FpSpread1.ActiveSheet.Columns[3].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                NumberCellType intcell = new NumberCellType();
                intcell.SpinButton = true;
                intcell.DecimalPlaces = 0;
                FpSpread1.ActiveSheet.Cells[0, 3, FpSpread1.ActiveSheet.RowCount - 1, 3].CellType = intcell;
                FpSpread1.ActiveSheet.Columns[3].Width = 45;

                //Column 4 - Unit Price 
                //Set as Currency 
                FpSpread1.ActiveSheet.Columns[4].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                CurrencyCellType curcell = new CurrencyCellType();
                FpSpread1.ActiveSheet.Cells[0, 4, FpSpread1.ActiveSheet.RowCount - 1, 4].CellType = curcell;
                FpSpread1.ActiveSheet.Columns[4].Width = 66;

                //Column 5 - Size 
                //Set as option 
                FpSpread1.ActiveSheet.Columns[5].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                MultiOptionCellType multiType = new MultiOptionCellType();
                multiType.Items = new string[] { "红色", "绿色" };
                multiType.Orientation = FarPoint.Win.RadioOrientation.Horizontal;
                FpSpread1.ActiveSheet.Cells[0, 5, FpSpread1.ActiveSheet.RowCount - 1, 5].CellType = multiType;
                FpSpread1.ActiveSheet.Cells[0, 5, FpSpread1.ActiveSheet.RowCount - 1, 5].Font = new Font("微软雅黑", 8, FontStyle.Regular);
                FpSpread1.ActiveSheet.Cells[0, 5, FpSpread1.ActiveSheet.RowCount - 1, 5].ForeColor = Color.Blue;
                FpSpread1.ActiveSheet.Columns[5].Width = 110;

                //SUPPLIERS 
                {
                    //Contact 
                    FpSpread1.ActiveSheet.Columns[6].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                    FpSpread1.ActiveSheet.Cells[0, 6, FpSpread1.ActiveSheet.RowCount - 1, 6].Font = new Font("微软雅黑", 10, FontStyle.Italic);
                    FpSpread1.ActiveSheet.Columns[6].Width = 125;


                    //Column 7: Web Site 
                    //HyperLink cell 
                    FpSpread1.ActiveSheet.Columns[7].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                    HyperLinkCellType hyperType = new HyperLinkCellType();
                    hyperType.Link = "http://www.gcpowertools.com.cn";
                    hyperType.Text = "GCPowerTools";
                    hyperType.LinkArea = new LinkArea(0, hyperType.Text.Length);
                    FpSpread1.ActiveSheet.Cells[0, 7, FpSpread1.ActiveSheet.RowCount - 1, 7].CellType = hyperType;
                    FpSpread1.ActiveSheet.Columns[7].Width = 70;

                    //Column 8: Phone 
                    //Mask 
                    FpSpread1.ActiveSheet.Columns[8].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                    MaskCellType picType = new MaskCellType();
                    picType.Mask = "(###) ####-###";
                    picType.MaskChar = '_';
                    FpSpread1.ActiveSheet.Cells[0, 8, FpSpread1.ActiveSheet.RowCount - 1, 8].CellType = picType;
                    FpSpread1.ActiveSheet.Columns[8].Width = 100;

                    FpSpread1.ActiveSheet.Cells[0, 8, FpSpread1.ActiveSheet.RowCount - 1, 8].Font = new Font("微软雅黑", 8, FontStyle.Regular);


                    //Column 9: Date 
                    FpSpread1.ActiveSheet.Columns[9].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
                    FpSpread1.ActiveSheet.Columns[9].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
                    DateTimeCellType dtType = new DateTimeCellType();
                    dtType.DateTimeFormat = DateTimeFormat.ShortDate;
                    FpSpread1.ActiveSheet.Cells[0, 9, FpSpread1.ActiveSheet.RowCount - 1, 9].CellType = dtType;
                    FpSpread1.ActiveSheet.Columns[9].Width = 75;
                    FpSpread1.ActiveSheet.Cells[0, 9, FpSpread1.ActiveSheet.RowCount - 1, 9].Font = new Font("微软雅黑", 8, FontStyle.Regular);
                }

                //Populate with sample data 
                string[,] ary2 = new string[6, 17];
                ary2 = (string[,])GetData();
                for (i = 0; i <= 15; i++)
                {
                    //Product 
                    FpSpread1.ActiveSheet.Cells[i, 1].Value = ary2[0, i];
                    //Unit Price 
                    FpSpread1.ActiveSheet.Cells[i, 4].Value = ary2[1, i];
                    //Qty 
                    FpSpread1.ActiveSheet.Cells[i, 3].Value = ary2[5, i];

                    //Contact 
                    FpSpread1.ActiveSheet.Cells[i, 6].Value = ary2[3, i];
                    //Phone - ary2(2, i) 
                    FpSpread1.ActiveSheet.Cells[i, 8].Value = ary2[2, i];
                    //Order Date 

                    FpSpread1.ActiveSheet.Cells[i, 9].Text = ary2[4, i];
                }


                //Init all to in stock 
                for (i = 0; i <= FpSpread1.ActiveSheet.RowCount - 1; i++)
                {
                    FpSpread1.ActiveSheet.Cells[i, 2].Value = true;
                }

                //Add totals row 
                int lastrow = FpSpread1.ActiveSheet.RowCount - 1;
                FpSpread1.ActiveSheet.Rows[lastrow].BackColor = Color.Yellow;
                for (i = 0; i <= FpSpread1.ActiveSheet.Columns.Count - 1; i++)
                {
                    FpSpread1.ActiveSheet.Cells[lastrow, i].CellType = new FarPoint.Win.Spread.CellType.GeneralCellType();
                    FpSpread1.ActiveSheet.Cells[lastrow, i].Text = "";
                }


                //Summary Row 
                FpSpread1.ActiveSheet.Cells[lastrow, 1].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Right;
                FpSpread1.ActiveSheet.Cells[lastrow, 1].Text = "清单:";

                FpSpread1.ActiveSheet.Cells[lastrow, 3].Formula = "SUM(D1:D" + lastrow + ")";
            }

            FpSpread1.AllowUserFormulas = true;

        }
        public Array GetData()
        {
            //Generate sample data 

            string[,] ary = new string[6, 17];
            object fp = "029";

            //Product 
            ary[0, 0] = "三明治";
            ary[0, 1] = "奶酪";
            ary[0, 2] = "糖浆";
            ary[0, 3] = "调味品";
            ary[0, 4] = "豆豉";
            ary[0, 5] = "青椒";
            ary[0, 6] = "栗子";
            ary[0, 7] = "寿司";
            ary[0, 8] = "牛肉";
            ary[0, 9] = "海鱼";
            ary[0, 10] = "土豆";
            ary[0, 11] = "番茄酱";
            ary[0, 12] = "虾酱";
            ary[0, 13] = "豆腐";
            ary[0, 14] = "转基因大米";
            ary[0, 15] = "奶油水果饼";

            //Unit Price 
            ary[1, 0] = "13";
            ary[1, 1] = "2.6";
            ary[1, 2] = "100";
            ary[1, 3] = "19.50";
            ary[1, 4] = "8.75";
            ary[1, 5] = "1.25";
            ary[1, 6] = "36.35";
            ary[1, 7] = "18.00";
            ary[1, 8] = "213";
            ary[1, 9] = "10.35";
            ary[1, 10] = "12.43";
            ary[1, 11] = "91.95";
            ary[1, 12] = "1.25";
            ary[1, 13] = "14";
            ary[1, 14] = "17.25";
            ary[1, 15] = "3";

            //Phone 
            ary[2, 0] = fp + "8833-600";
            ary[2, 1] = fp + "8833-600";
            ary[2, 2] = fp + "8833-600";
            ary[2, 3] = fp + "8833-600";
            ary[2, 4] = fp + "8833-600";
            ary[2, 5] = fp + "8833-600";
            ary[2, 6] = fp + "8833-600";
            ary[2, 7] = fp + "8833-600";
            ary[2, 8] = fp + "8833-600";
            ary[2, 9] = fp + "8833-600";
            ary[2, 10] = fp + "8833-600";
            ary[2, 11] = fp + "8833-600";
            ary[2, 12] = fp + "8833-600";
            ary[2, 13] = fp + "8833-600";
            ary[2, 14] = fp + "8833-600";
            ary[2, 15] = fp + "8833-600";

            //Suppliers 
            ary[3, 0] = "汪洋";
            ary[3, 1] = "钱程";
            ary[3, 2] = "赵庆 ";
            ary[3, 3] = "胡明";
            ary[3, 4] = "陈亮";
            ary[3, 5] = "胡强";
            ary[3, 6] = "孙鹏";
            ary[3, 7] = "郝时光";
            ary[3, 8] = "郭春光";
            ary[3, 9] = "张良辰";
            ary[3, 10] = "路红玉";
            ary[3, 11] = "赵婕妤";
            ary[3, 12] = "翁杨依";
            ary[3, 13] = "王思达";
            ary[3, 14] = "陈之路";
            ary[3, 15] = "陈翔宇";

            //Order Date 
            ary[4, 0] = "7/3/2004";
            ary[4, 1] = "8/30/2004";
            ary[4, 2] = "7/14/2004";
            ary[4, 3] = "7/6/2004";
            ary[4, 4] = "8/1/2004";
            ary[4, 5] = "8/23/2004";
            ary[4, 6] = "8/06/2004";
            ary[4, 7] = "8/12/2004";
            ary[4, 8] = "7/27/2004";
            ary[4, 9] = "7/01/2004";
            ary[4, 10] = "8/9/2004";
            ary[4, 11] = "8/7/2004";
            ary[4, 12] = "7/13/2004";
            ary[4, 14] = "7/26/2004";
            ary[4, 14] = "8/10/2004";
            ary[4, 15] = "8/12/2004";

            //Qty 
            ary[5, 0] = "10";
            ary[5, 1] = "125";
            ary[5, 2] = "18";
            ary[5, 3] = "937";
            ary[5, 4] = "892";
            ary[5, 5] = "3";
            ary[5, 6] = "11";
            ary[5, 7] = "83";
            ary[5, 8] = "39";
            ary[5, 9] = "92";
            ary[5, 10] = "27";
            ary[5, 11] = "34";
            ary[5, 12] = "18";
            ary[5, 14] = "8";
            ary[5, 14] = "189";
            ary[5, 15] = "112";


            return ary;
        }

        private void ckVisible_CheckedChanged(object sender, EventArgs e)
        {
            FpSpread1.ActiveSheet.PreviewRowInfo.Visible = ckVisible.Checked;
        }

        private void btnBackColor_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = FpSpread1.ActiveSheet.PreviewRowInfo.BackColor;
            colorDialog1.ShowDialog();
            if (!colorDialog1.Color.Equals(FpSpread1.ActiveSheet.PreviewRowInfo.BackColor))
            {
                FpSpread1.ActiveSheet.PreviewRowInfo.BackColor = colorDialog1.Color;
                FpSpread1.Refresh();
            }
        }

        private void btnForeColor_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = FpSpread1.ActiveSheet.PreviewRowInfo.ForeColor;
            colorDialog1.ShowDialog();
            if (!colorDialog1.Color.Equals(FpSpread1.ActiveSheet.PreviewRowInfo.ForeColor))
            {
                FpSpread1.ActiveSheet.PreviewRowInfo.ForeColor = colorDialog1.Color;
                FpSpread1.Refresh();
            }
        }

        private void cboAlignment_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboAlignment.SelectedItem.ToString())
            {
                case "Left":
                    FpSpread1.ActiveSheet.PreviewRowInfo.HorizontalAlignment = FarPoint.Win.HorizontalAlignment.Left;
                    break;
                case "Center":
                    FpSpread1.ActiveSheet.PreviewRowInfo.HorizontalAlignment = FarPoint.Win.HorizontalAlignment.Center;
                    break;
                case "Right":
                    FpSpread1.ActiveSheet.PreviewRowInfo.HorizontalAlignment = FarPoint.Win.HorizontalAlignment.Right;
                    break;
                case "Justify":
                    FpSpread1.ActiveSheet.PreviewRowInfo.HorizontalAlignment = FarPoint.Win.HorizontalAlignment.Justify;
                    break;
                case "Distributed":
                    FpSpread1.ActiveSheet.PreviewRowInfo.HorizontalAlignment = FarPoint.Win.HorizontalAlignment.Distributed;
                    break;
                default:
                    return;
            }
        }

        private void numericUpDownColumnIndex_ValueChanged_1(object sender, EventArgs e)
        {
            FpSpread1.ActiveSheet.PreviewRowInfo.ColumnIndex = Convert.ToInt16(numericUpDownColumnIndex.Value);
        }
    }
}
